﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Prod;
using ESOL.Common;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageTechnicStation : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private ProdTechnicEntity mProdStation;
        #endregion

        #region Constructor
        public frmManageTechnicStation()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void Init()
        {
            BindComboBox();
            BindGridData();
        }

        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByEnum(cmbQryDeletedFlag, typeof(DeleteFlag),typeof(int),true);
            UICommon.UICommonFunctions.BindGridComboboxByEnum(colDeletedFlag, typeof(DeleteFlag), typeof(int), true);
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStationType, "STT", true);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(colStationType, "STT", false);
        }

        private string GetCondition()
        {
            string condition = "";
            if (!string.IsNullOrEmpty(txtQryStationName.Text))
            {
                condition += " AND A.TECHNIC_NAME LIKE '%" + txtQryStationName.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtQryStationCode.Text))
            {
                condition += " AND A.TECHNIC_CODE LIKE '%" + txtQryStationCode.Text + "%'";
            }
            if (!string.IsNullOrEmpty(cmbQryDeletedFlag.Text) && cmbQryDeletedFlag.SelectedValue != null)
            {
                condition += " AND A.DELETE_FLAG = '" + cmbQryDeletedFlag.SelectedValue.ToString() + "' ";
            }
            if (!string.IsNullOrEmpty(cmbStationType.SelectedValue.ToString()))
            {
                condition += " AND A.STATION_TYPE = '" + cmbStationType.SelectedValue.ToString() + "' ";
            }
            return condition;
        }

        private void BindGridData()
        {
            string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "";
            DataTable dt = ProdTechnicBs.GetProdTechnicByCondition(rowCount, condition, orderBy);
            gvMain.DataSource = dt;
        }

        private bool DeleteSupplier()
        {
            bool bFlag = false;
            if (mProdStation != null)
            {
                mProdStation.DeleteFlag = 1;
                bFlag = ProdTechnicBs.UpdateProdTechnic(mProdStation);
            }
            return bFlag;
        }
        #endregion

        #region Event
        protected override void Click_View()
        {
            frmMaintainTechnicStation frm = new frmMaintainTechnicStation();
            frm.ThisActionType = ActionType.View;
            frm.mProdStation = this.mProdStation;
            frm.ShowDialog();
        }

        protected override void Click_New()
        {
            frmMaintainTechnicStation frm = new frmMaintainTechnicStation();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
                BindGridData();
        }

        protected override void Click_Modify()
        {
            if (mProdStation != null)
            {
                frmMaintainTechnicStation frm = new frmMaintainTechnicStation();
                frm.mProdStation = mProdStation;
                frm.ThisActionType = ActionType.Modify;
                DialogResult dr = frm.ShowDialog();
                if (dr == DialogResult.OK)
                    BindGridData();
            }
        }

        protected override void Click_Delete()
        {
            if (mProdStation != null)
            {
                DialogResult dr = MessageBox.Show("是否确认删除?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.Yes)
                {
                    if (DeleteSupplier())
                    {
                        MessageBox.Show("删除成功!");
                        BindGridData();
                    }
                    else MessageBox.Show("删除失败!");
                }
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            Init();
        }

        protected override void Click_Query()
        {
            BindGridData();
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mProdStation = new ProdTechnicEntity(dr);
            switch (mProdStation.DeleteFlag)
            {
                case 0:
                    btnDelete.Enabled = true;
                    break;
                case 1:
                    btnDelete.Enabled = false;
                    break;
            }
        }

        private void btnTechnicCost_Click(object sender, EventArgs e)
        {
            if (mProdStation == null)
            {
                MessageBox.Show("请选择工艺站别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainTechnicCost frm = new frmMaintainTechnicCost();
            frm.prodTechnicEntity = mProdStation;
            frm.ShowDialog();
        }

        private void btnRatio_Click(object sender, EventArgs e)
        {
            if (mProdStation == null)
            {
                MessageBox.Show("请选择工艺站别！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainTechnicRatio frm = new frmMaintainTechnicRatio();
            frm.prodTechnicEntity = mProdStation;
            frm.ShowDialog();
        }

        #endregion
    }
}
